﻿using System;
using System.Collections.Generic;
using Framework.Owin.RequestLog;
using Framework.Owin.WebApiSecurity;
using Owin;

namespace Framework.Owin
{
    public static class AppBuilderExtensions
    {
        /// <summary>
        /// 请求日志
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="requestLogProceAction">日志处理</param>
        /// <returns></returns>
        public static IAppBuilder UseRequestLog(this IAppBuilder builder, 
            Action<string> requestLogProceAction)
        {
            //USE可以带多个参数，对应中间件构造函数中的第2、3、....参数;
            return builder.Use<RequestLogMiddleware>(requestLogProceAction);
        }

        /// <summary>
        /// IP访问限制
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="getIpListfFunc">得到指定ip访问列表</param>
        /// <param name="setIpListAction">设置指定ip访问记录</param>
        /// <param name="delIpListAction">删除指定ip所有访问记录</param>
        /// <param name="getIpBlackFunc">得到指定ip黑名单</param>
        /// <param name="setIpBlackAction">设置指定ip黒名单</param>
        /// <param name="delIpBlackAction">删除指定ip黑名单</param>
        /// <returns></returns>
        public static IAppBuilder UseIpLimit(this IAppBuilder builder, 
            Func<string, List<VisitLog>> getIpListfFunc,
            Action<string, List<VisitLog>> setIpListAction,
            Action<string> delIpListAction,
            Func<string, IpBlack> getIpBlackFunc,
            Action<string,IpBlack> setIpBlackAction,
            Action<string> delIpBlackAction)
        {
            //USE可以带多个参数，对应中间件构造函数中的第2、3、....参数;
            return builder.Use<IPLimitMiddleware>(getIpListfFunc, setIpListAction, delIpListAction, getIpBlackFunc, setIpBlackAction, delIpBlackAction);
        }
    }
}
